class Solution:
    def largestPalindrome(self, n: int) -> int:
        if n == 1:
            return 9
        mi, ma = 10 ** (n - 1), 10 ** n - 1
        for i in range(ma, mi - 1, -1):
            ans = int(str(i) + str(i)[::-1])
            x = ma
            while x * x <= ans:
                if ans % x == 0:
                    return ans % 1337
                x -= 1